package com.atguigu.userprofile.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.userprofile.bean.TagCondition;
import com.atguigu.userprofile.bean.TaskInfo;
import com.atguigu.userprofile.bean.UserGroup;
import com.atguigu.userprofile.service.UserGroupService;
import com.atguigu.userprofile.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2021-05-04
 */
@RestController
public class UserGroupController {

    @Autowired
    UserGroupService userGroupService;

    @RequestMapping("/user-group-list")
    @CrossOrigin
    public String  getUserGroupList(@RequestParam("pageNo")int pageNo , @RequestParam("pageSize") int pageSize){
        int startNo=(  pageNo-1)* pageSize;
        int endNo=startNo+pageSize;

        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        int count = userGroupService.count(queryWrapper);

        queryWrapper.orderByDesc("id").last(" limit " + startNo + "," + endNo);
        List<UserGroup> userGroupList = userGroupService.list(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail",userGroupList);
        jsonObject.put("total",count);

        return  jsonObject.toJSONString();
    }

//接收请求
//调用服务层
//返回结果

    @PostMapping("/user-group")
    public String saveUserGroup(@RequestBody UserGroup userGroup){
        //1、 分群的基本信息保存在mysql中 ，数据表已经在mysql中建立
       userGroupService.saveUserGroup(userGroup);  //生成主键


       // 2    根据条件生成sql语句，操作clickhouse ，实现bitmap计算，并把计算结果写入clickhouse中

        userGroupService.genUserGroupUs(userGroup);

//        if (true){
//            throw new RuntimeException("卡住页面，测试");
//        }
       // 3  为了应对下游系统的高qps查询 ，把计算结果放redis一份
        userGroupService.saveUserGroupToRedis(userGroup,false);

        userGroupService.saveOrUpdate(userGroup);  //补充人数



        return  "success";
    }

    @PostMapping("/user-group-evaluate")
    public Long userGroupEvaluate(@RequestBody UserGroup userGroup){

        Long userGroupNum = userGroupService.evaluateUserGroup(userGroup);
        return userGroupNum;
    }



    @PostMapping("/user-group-refresh/{id}")
    public String refreshUserGroup(@PathVariable("id") String id ,
                                   @RequestParam("busiDate") String busiDate){

           userGroupService.refreshUserGroup(  id ,   busiDate);

            return  "success";
    }



}

